<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>redis发布者订阅者 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="发布者订阅者发布与订阅（publish&#x2F;subscribe）模式，简称为pub&#x2F;sub模式，是一种消息通信模式 , 发送者(pub)发送信息, 订阅者(sub)订阅消息。redis也实现了这种模式。  redis提供的发布订阅命令 subscribe：subscribe channel [channel …] 订阅一个或多个频道 unsubscribe：unsubscribe [channel [">
<meta property="og:type" content="article">
<meta property="og:title" content="redis发布者订阅者">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="发布者订阅者发布与订阅（publish&#x2F;subscribe）模式，简称为pub&#x2F;sub模式，是一种消息通信模式 , 发送者(pub)发送信息, 订阅者(sub)订阅消息。redis也实现了这种模式。  redis提供的发布订阅命令 subscribe：subscribe channel [channel …] 订阅一个或多个频道 unsubscribe：unsubscribe [channel [">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-31T16:00:00.000Z">
<meta property="article:modified_time" content="2021-02-01T09:35:36.035Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-redis发布者订阅者" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" class="article-date">
  <time class="dt-published" datetime="2021-01-31T16:00:00.000Z" itemprop="datePublished">2021-02-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      redis发布者订阅者
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="发布者订阅者"><a href="#发布者订阅者" class="headerlink" title="发布者订阅者"></a>发布者订阅者</h2><h3 id="发布与订阅（publish-subscribe）模式，简称为pub-sub模式，是一种消息通信模式-发送者-pub-发送信息-订阅者-sub-订阅消息。redis也实现了这种模式。"><a href="#发布与订阅（publish-subscribe）模式，简称为pub-sub模式，是一种消息通信模式-发送者-pub-发送信息-订阅者-sub-订阅消息。redis也实现了这种模式。" class="headerlink" title="发布与订阅（publish/subscribe）模式，简称为pub/sub模式，是一种消息通信模式 , 发送者(pub)发送信息, 订阅者(sub)订阅消息。redis也实现了这种模式。"></a>发布与订阅（publish/subscribe）模式，简称为pub/sub模式，是一种消息通信模式 , 发送者(pub)发送信息, 订阅者(sub)订阅消息。redis也实现了这种模式。</h3><br>

<h2 id="redis提供的发布订阅命令"><a href="#redis提供的发布订阅命令" class="headerlink" title="redis提供的发布订阅命令"></a>redis提供的发布订阅命令</h2><ul>
<li>subscribe：subscribe channel [channel …] 订阅一个或多个频道</li>
<li>unsubscribe：unsubscribe [channel [channel …]] 退订频道，如果没有指定频道，则退订所有的频道</li>
<li>publish：publish channel message 给指定的频道发消息</li>
<li>psubscribe：psubscribe pattern [pattern …] 订阅给定模式相匹配的所有频道</li>
<li>punsubscribe：punsubscribe [pattern [pattern …]] 退订给定的模式，如果没有指定模式，则退订所有模式</li>
</ul>
<hr>
<br>

<h2 id="发布者"><a href="#发布者" class="headerlink" title="发布者"></a>发布者</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">import redis</span><br><span class="line"></span><br><span class="line">conn &#x3D; redis.Redis(host&#x3D;&#39;127.0.0.1&#39;, decode_responses&#x3D;True)</span><br><span class="line"></span><br><span class="line">conn.publish(&#39;xiaoming&#39;, &#39;18&#39;)</span><br></pre></td></tr></table></figure>
<h2 id="订阅者"><a href="#订阅者" class="headerlink" title="订阅者"></a>订阅者</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">import redis</span><br><span class="line"></span><br><span class="line">conn &#x3D; redis.Redis(host&#x3D;&#39;127.0.0.1&#39;, decode_responses&#x3D;True)</span><br><span class="line"></span><br><span class="line"># 生成一个订阅者对象</span><br><span class="line">pubsub &#x3D; conn.pubsub()</span><br><span class="line"></span><br><span class="line"># 订阅一个消息</span><br><span class="line">pubsub.subscribe(&#39;xiaoming&#39;)</span><br><span class="line"></span><br><span class="line"># 创建一个接收</span><br><span class="line">while True:</span><br><span class="line">    print(&#39;working~~~&#39;)</span><br><span class="line">    msg &#x3D; pubsub.parse_response()</span><br><span class="line">    print(msg)</span><br></pre></td></tr></table></figure>
<hr>
<br>

<ul>
<li>运行多个订阅者，每个订阅者会等待接收发布者发送的消息，当发布者发布消息后，订阅者全部接收到</li>
</ul>
<hr>
<br>

<h2 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h2><h3 id="所有pub-sub很有用，但是redis的实现有这么几个问题："><a href="#所有pub-sub很有用，但是redis的实现有这么几个问题：" class="headerlink" title="所有pub/sub很有用，但是redis的实现有这么几个问题："></a>所有pub/sub很有用，但是redis的实现有这么几个问题：</h3><ol>
<li>如果一个客户端订阅了频道，但自己读取消息的速度却不够快的话，那么不断积压的消息会使redis输出缓冲区的体积变得越来越大，这可能使得redis本身的速度变慢，甚至直接崩溃。 </li>
<li>这和数据传输可靠性有关，如果在订阅方断线，那么他将会丢失所有在短线期间发布者发布的消息，这个让绝不多数人都很失望吧。</li>
</ol>
<hr>
<br>

<h2 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景:"></a>使用场景:</h2><ol>
<li><p>实时消息系统</p>
</li>
<li><p>实时聊天</p>
</li>
<li><p>订阅,关注系统</p>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" data-id="ckkt9opft0004i4twh38cefxx" data-title="redis发布者订阅者" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          redis键空间事件
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/31/axios%E5%B0%81%E8%A3%85/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">axios封装</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>